package com.example.musicplayer.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.widget.Toast;

import com.example.musicplayer.dao.IMusicHandler;
import com.example.musicplayer.dao.MusicHandler;
import com.example.musicplayer.dto.Song;

/**
 * Class for methods to play music also other logic, skip, repeat, progress, etc
 * 
 */
public class MusicServiceStub extends Service implements IMusicService, OnCompletionListener {

	// Use Android media player
	MediaPlayer mp;
	List<Song> songList;
	IMusicHandler mHandler;
	
	@Override
	public void onCreate() {		
		mp = new MediaPlayer();
		mHandler = new MusicHandler(getContentResolver(),false, this);
		songList = mHandler.GetMusicList(false);
	}

	int songIndex = 0;
	boolean isShuffle = false;
	boolean isRepeat = false;
	
	//string used to start service from list activity
	 public static final String ACTION_TOGGLE_PLAYBACK = "com.example.android.musicplayer.action.SONGTOBEPLAYED";
	 public static final String ACTION_PLAY = "com.example.android.musicplayer.action.PLAY";
	 public static final String ACTION_PAUSE = "com.example.android.musicplayer.action.PAUSE";
	 public static final String ACTION_FORWARD = "com.example.android.musicplayer.action.FORWARD";
	 public static final String ACTION_BACKWARD = "com.example.android.musicplayer.action.BACKWARD";
	 public static final String ACTION_STOP = "com.example.android.musicplayer.action.STOP";
	 public static final String ACTION_SHUFFLE = "com.example.android.musicplayer.action.SHUFFLE";
	 public static final String ACTION_REPEAT = "com.example.android.musicplayer.action.REPEAT";

	 
	/**
	 * Method to play music, takes SongIndex
	 */
	public void playMusic(int songIndex) {
		try {
			mp.reset();
			mp.setDataSource(songList.get(songIndex).getSongPath());
			mp.prepare();
			mp.start();
			// Displaying Song title
			String songTitle = songList.get(songIndex).getSongName();
			// UI - set some label equal to songTitle

			// set Progress bar values

			// Update progress bar

		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * method to pause music
	 */
	public void pauseMusic() {
		mp.pause();
	}

	/**
	 * method to stop music
	 */
	public void stopMusic() {
		mp.stop();
	}

	/**
	 * method to skip song
	 */
	public void skipForward() {
		// Check if next song is there
		if (songIndex < (songList.size() - 1)) {
			playMusic(songIndex + 1);
			songIndex = songIndex + 1;
		} else {
			// play first song
			playMusic(0);
			songIndex = 0;
		}
	}

	/**
	 * method to skip song backward
	 */
	public void skipBackward() {
		// Check if song index isn't 0
		if (songIndex > 0) {
			playMusic(songIndex - 1);
			songIndex = songIndex - 1;
		} else {
			// play last song
			playMusic(songList.size() - 1);
			songIndex = songList.size() - 1;
		}
	}
	

	/**
	 * method to check for song completion
	 */
	@Override
	public void onCompletion(MediaPlayer mp) {

		if (isRepeat) {
			// repeat is on play same song again
			playMusic(songIndex);
		} else if (isShuffle) {
			// shuffle is on - play a random song
			Random rand = new Random();
			songIndex = rand.nextInt((songList.size() - 1) - 0 + 1) + 0;
			playMusic(songIndex);
		} else {
			// no repeat or shuffle ON - play next song
			if (songIndex < (songList.size() - 1)) {
				playMusic(songIndex + 1);
				songIndex = songIndex + 1;
			} else {
				// play first song
				playMusic(0);
				songIndex = 0;
			}
		}
	}
	
    @Override
    public void onDestroy(){
    super.onDestroy();
       mp.release();
    }

	/**
	 * method to shuffle songs
	 */
	public void shuffle() {
		if (isShuffle) {
			isShuffle = false;
		} else {
			isShuffle = true;
			isRepeat = false;
		}
	}

	/**
	 * method to repeat song
	 */
	public void repeat() {
		if (isRepeat) {
			isRepeat = false;
			// Repeat is off
		} else {
			// make repeat to true
			isRepeat = true;
			// make shuffle to false
			isShuffle = false;
		}

	}
	
	/**
	 * Method that gets called when users is finshed listening to music
	 * will stop music and save last song played and current song play postion
	 */
	public void close(){
		stopMusic();
		//Add Save state 
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
    public int onStartCommand(Intent i,int flags, int startId) {
        String action = i.getAction();
        //if (action.equals(ACTION_TOGGLE_PLAYBACK)) processTogglePlaybackRequest();
        if (action.equals(ACTION_PLAY))playMusic(songIndex);
        else if (action.equals(ACTION_PAUSE)) pauseMusic();
        else if (action.equals(ACTION_FORWARD)) skipForward();
        else if (action.equals(ACTION_BACKWARD)) skipBackward();
        else if (action.equals(ACTION_STOP)) stopMusic();
        else if (action.equals(ACTION_REPEAT)) repeat();
        else if (action.equals(ACTION_SHUFFLE)) shuffle();
        
        return START_NOT_STICKY; 
    }

}
